﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Walson.Net
{
    /// <summary>
    /// 数据包信息
    /// </summary>
    public class Packet
    {
        /// <summary>
        /// 远端节点的相关信息
        /// </summary>
        public IPEndPoint RemoteEndPoint
        {
            get;
            private set;
        }

        /// <summary>
        /// 数据包内容
        /// </summary>
        public byte[] Data
        {
            get;
            private set;
        }

        /// <summary>
        /// 数据包
        /// </summary>
        /// <param name="ip">远端节点的 IP 地址</param>
        /// <param name="port">远端节点的端口</param>
        /// <param name="data">数据</param>
        /// <param name="dataLen">数据长度</param>
        public Packet(IPAddress ip, int port, byte[] data, int dataLen)
        {
            if(ip==null)
                RemoteEndPoint = new IPEndPoint(IPAddress.Any, port);
            else
                RemoteEndPoint = new IPEndPoint(ip, port);
            Data = new byte[dataLen];
            Array.Copy(data, 0, Data, 0, dataLen);
        }

        /// <summary>
        /// 数据包
        /// </summary>
        /// <param name="ip">远端节点的 IP 地址</param>
        /// <param name="port">远端节点的端口号</param>
        /// <param name="data">数据</param>
        public Packet(IPAddress ip, int port, byte[] data)
            : this(ip, port, data, data.Length)
        {

        }

        /// <summary>
        /// 不指定远端 IP 地址的数据包
        /// </summary>
        /// <param name="port">远端节点的端口号</param>
        /// <param name="data">数据</param>
        public Packet(int port, byte[] data)
            : this(IPAddress.Any, port, data, data.Length)
        {

        }
    }
}
